//
//  visitorLoginView.swift
//  我的微博
//
//  Created by 杨庆 on 15/10/10.
//  Copyright © 2015年 双元班. All rights reserved.
//

import UIKit
/// 访客视图的协议
protocol visitorLoginViewDelegate : NSObjectProtocol{
    ///将要登录;
    func visitorLoginViewWillLogin()
    ///将要注册;
    func visitorLoginViewWillRegister()
}
/// 访客的视图
class visitorLoginView: UIView {
    //定义代理;
    weak var delegate : visitorLoginViewDelegate?
//MARK:-执行按钮的代理方法;
    func clickLogin(){
        delegate?.visitorLoginViewWillLogin()
        
    }
    func clickregister(){
        delegate?.visitorLoginViewWillRegister()
    }
//MARK:-设置每个界面视图的信息;
    func setupViewInfo(isHome:Bool,imagName:String,message : String){
        messageLabel.text = message
        iconView.image = UIImage(named: imagName)
        homeIconView.hidden = !isHome
        //如果不是,主登录的界面的,就将 mask 的view 挪动到后面;
        isHome ? startAnimation() : sendSubviewToBack(maskIconView)
    }
    //开始转盘的旋转;
    private func startAnimation(){
    //要实现,就要求应用核心动画来实现;
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        anim.toValue = 2 * M_PI
        anim.repeatCount = MAXFLOAT
        anim.duration = 20.0
    //进行的无限循环的动画的效果;(提示: 我们日后的开发的当中,如果应用了核心的动画,一定要检查退出后的效果)
        anim.removedOnCompletion = false
    //添加核心的动画,还是很好用的;
        iconView.layer.addAnimation(anim, forKey: nil)
        //homeIconView.layer.addAnimation(anim, forKey: nil)
    }
//MARK:-手工写的代码的方法;
        override init(frame : CGRect){
    super.init(frame: frame)
            setupUI()
    }
//MARK:-非sb和xib使用的此类的方法;
    required init?(coder aDecoder: NSCoder) {
        //禁止用 sb 和 xib 使用本类;
        //fatalError("init(coder:) has not been implemented"
        super.init(coder: aDecoder)
        setupUI()
    }
//MARK:1-设置界面;
    private func setupUI() {
        //1.添加界面;
        addSubview(iconView)
        addSubview(maskIconView)
        addSubview(homeIconView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        // 2.自动布局,来设置frame 来添加约束,来实现 个个机型的适配;
        //如果要使用 自动布局系统的布局,需要设置一个属性,不设置的话就会 崩掉的;
        //一旦加入自动布局的系统 就不需要设置 frame;
        iconView.translatesAutoresizingMaskIntoConstraints=false
        //设置 图标;
       addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: -40))
        //设置 小房子;
        homeIconView.translatesAutoresizingMaskIntoConstraints=false
        //设置一个 统一的合体的参照;
        addConstraint(NSLayoutConstraint(item: homeIconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: homeIconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: 0))
        //设置消息的文字
        messageLabel.translatesAutoresizingMaskIntoConstraints=false
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: 100))
        //设置宽度;
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 230))
        // 设置注册的按钮
        registerButton.translatesAutoresizingMaskIntoConstraints=false
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Left, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 16))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 35))
        // 设置登录按钮
        loginButton.translatesAutoresizingMaskIntoConstraints=false
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Right, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 16))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item:loginButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 35))
        // 设置遮罩的视图
        //应用的是 constranints ,VFL 的 可视化的格式的语言;
        maskIconView.translatesAutoresizingMaskIntoConstraints=false
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[subview]-0-|", options: NSLayoutFormatOptions( rawValue: 0) , metrics: nil, views: ["subview":maskIconView]))
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[subview]-170-|", options: NSLayoutFormatOptions( rawValue: 0) , metrics: nil, views: ["subview":maskIconView]))
        //3.设置背景的颜色
        backgroundColor = UIColor(white: 237.0/255.0, alpha: 1.0)
    }
// MARK:2-懒加载控件;
    ///图标
    private lazy var iconView : UIImageView = {
    let iv = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    return iv
    }()
    /// 添加遮罩视图;
    private lazy var maskIconView: UIImageView = {
        let iv = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
        return iv
        }()
    /// 小房子
    private lazy var homeIconView : UIImageView = {
        let iv = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
        return iv
    }()
    /// 消息文字
    private lazy var messageLabel : UILabel = {
    let label = UILabel()
        label.text = "关注一些人,回到这里看看有什么惊喜关注一些人,回到这里看看有什么惊喜"
        label.textColor=UIColor.darkGrayColor()
        label.font=UIFont.systemFontOfSize(14)
        label.numberOfLines = 0
        label.sizeToFit()
        return label
    }()
    /// 注册按钮
    private lazy var registerButton : UIButton = {
    let button = UIButton()
        button.setTitle("注册", forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        button.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        button.addTarget(self, action: "clickregister", forControlEvents: UIControlEvents.TouchUpInside)
    return button
    }()
    /// 登录按钮
    private lazy var loginButton : UIButton = {
        let button = UIButton()
        button.setTitle("登录", forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        button.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Normal)
        button.addTarget(self, action: "clickLogin", forControlEvents: UIControlEvents.TouchUpInside)
        return button
        }()
}
